package com.example.controller;

import com.example.pojo.Result;
import com.example.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@Slf4j
public class UploadController {

    @Autowired
    private  AliOSSUtils aliOSSUtils;
//    @PostMapping("/upload")
    /**
     * 本地存储文件
     * MultipartFile 服务器端接收文件
     */
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传：{},{},{}",username,age,image);
//        //获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//        //构造唯一的文件名（不能重复 -uuid(通用唯一识别码)）1d2dbe18-5e54-4d6a-89d4-25990b07b65f
//        int index = originalFilename.lastIndexOf(",");
//        String extname = originalFilename.substring(index);
//        String newFileName = UUID.randomUUID().toString() + extname;
//        log.info("新的文件名：{}",newFileName);
//        //将文件存储在服务器的磁盘目录中D:\File
//        image.transferTo(new File("D:\\File\\"+newFileName));
//
//        return Result.success();
//    }

    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传，文件名:{}",image.getOriginalFilename());
        //调用OSS工具类
        String url = aliOSSUtils.upload(image);
        log.info("文件上传完成");
        return Result.success(url);
    }
}
